package com.mdhw.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class RandomUtil {

    private static final String RANDOM_STR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    private static final Random RANDOM = new Random();

    /**
     * 生成随机字符串
     * @param length 长度
     * @return
     */
    public static String getRandomStr(int length) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(RANDOM_STR.charAt(RANDOM.nextInt(RANDOM_STR.length())));
        }
        return sb.toString();
    }
    /**
     * 生成随机数 某个范围内
     * @param end    结束
     * @return
     */
    public static Integer getRandomInt(Integer end){
        Integer randomInt = RANDOM.nextInt(end);
        return randomInt;
    }


    /**
     * 某个范围内随机生成 N 个不重复的数  没有0
     * @param scope          范围
     * @param count          数量
     * @return
     */
    public static List<Integer>     randomStartToEnd(Long scope, Integer count ){
        Integer scopeInt = scope.intValue();
        Set<Integer> returnSet = new HashSet<>();
        while (returnSet.size() < count){
            Integer randomInt = RANDOM.nextInt(scopeInt )+1;
            returnSet.add(randomInt);
        }
        List<Integer> returnList = new ArrayList<>();
        for(Integer setItem:returnSet){
            returnList.add(setItem);
        }
        Collections.sort(returnList);
        return returnList;
    }

    /**
     *
     * @param scope          范围内
     * @param count          生成的随机数个数
     * @return
     */
    public static List<Integer> randomStartToEndNoContinuous(Long scope,Integer count){
        Integer scopeInt = scope.intValue();
        Integer divide = scopeInt / count;
        if(divide < 2){
            return randomStartToEnd(scope,count);
        }
        List<Integer> returnList = new ArrayList<>();
        Integer mod = scopeInt % count;
        // 分成 每 group 个 取 1个
        Integer group = scopeInt / count;
        List<Integer> groupList = new ArrayList<>();
        List<List<Integer>> groupItemList = new ArrayList<>();
        if(mod == 0){
            List<Integer> itemList = new ArrayList<>();
            for(Integer a= 1;a<=scopeInt;a++){
                itemList.add(a);
                if(itemList.size() >= group){
                    groupItemList.add(itemList);
                    itemList = new ArrayList<>();
                }
            }
        }else{
            Integer groupAddOne = group+1;
            // 分 count 组
            // 随机部分组里面 + 1个
            List<Integer> countModList = randomStartToEnd(count.longValue(), mod);
            Integer index = 1;
            for(Integer a=1;a<=count;a++){
                List<Integer> itemList = new ArrayList<>();
                if(countModList.contains(a)){
                    Integer lastIndex = index + groupAddOne;
                    for(Integer b=index;b<lastIndex;b++){
                        itemList.add(b);
                    }
                    index = lastIndex;
                }else{
                    Integer lastIndex = index + group;
                    for(Integer b=index;b<lastIndex;b++){
                        itemList.add(b);
                    }
                    index = lastIndex;
                }
                groupItemList.add(itemList);
            }
        }
        for(Integer a=0;a<groupItemList.size();a++){
            List<Integer> itemList = groupItemList.get(a);
            int randomI = RANDOM.nextInt(itemList.size());
            Integer randomInt = itemList.get(randomI);
            returnList.add(randomInt);
        }
        return returnList;
    }


}
